/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class SettingsStorePropertiesFile extends SettingsStoreMemory implements ThreadScheduable {
	public String filename;
	private boolean save = false,is_still_active=true;

	// THIS WILL KEEP THE LAST TIME THE PORPERTIES FILE WAS WRITTEN TO
	private File file = null;
	private long lastmodified = -1;

	public SettingsStorePropertiesFile(String p_filename) {
		super();
		filename = p_filename;
		load();
		file = new File(filename);
		ThreadSchedule.add(this);
	}

	public void save() {
		save = true;
		if (!is_still_active) {
			try {
				doWrite();
			} catch (Throwable th) {th.printStackTrace();}
		}
	}

	public boolean keepAlive() {return true;}

	public int sleepTime(){return 200;}

	private void doWrite() throws Throwable {
		FileOutputStream out = new FileOutputStream(filename);
		properties.store(out, "Settings");
		out.close();
		save = false;
	}

	public void process()
	throws Throwable {
		if(save) {
			save = false;
			doWrite();
			lastmodified = file.lastModified();
		} else {
			// CHECK THAT
			if (lastmodified == -1) {
				lastmodified = file.lastModified();
			} else {
				if (file.lastModified() > lastmodified) {
					// PROPERTIES FILE HAVE CHANGED SINCE LAST READ MUST RELOAD
					Log.trace.out("PROPERTIES FILE","File changed, now reloading");
					load();
					lastmodified = file.lastModified();
				}
			}
		}
	}

	public void close()
	throws Throwable {
		is_still_active= false;
		if(save) {
			save = false;
			doWrite();
		}
	}

	private void load() {
		try {
			File file=new File(filename);
			if (!file.exists()) {
				//Log.trace.out("New Settings file created","Config file = '" + file.getAbsolutePath() + "'." );
				file.createNewFile();
			}
			FileInputStream input = new FileInputStream(file);
			properties.load(input);
			input.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
			//Log.debug.out(ioe);
		}
	}

	public static void setSettings(String filename) {
		Settings.setSettingsStore(new SettingsStorePropertiesFile(filename));
	}
	/*

	public static class DelayedWrite extends Thread {
		private SettingsStorePropertiesFile store = null;
		private boolean running=false;

		public DelayedWrite(SettingsStorePropertiesFile store) {
			this.store = store;
		}

		public void run () {
			try {
				running = true;
				Thread.sleep(1000);
				FileOutputStream out = new FileOutputStream(store.filename);
				store.properties.store(out, "Settings");
				out.close();
				running = false;
			} catch (Throwable th) {
				Log.debug.out(th);
			}
		}
}*/

	public static void main(String args[]) throws Throwable {
		for (int i = 0;i < 10000;i++){
			Settings.getString("i"+i,"i"+i);
		}
		ThreadSchedule.haltBlocking();
		System.out.println("Done");
		System.exit(-1);
	}

}
